<template>
  <div class="app-container">
    <h3>文件上传测试页面</h3>
    
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>单文件上传测试</span>
      </div>
      <el-upload
        class="upload-demo"
        :action="uploadUrl"
        :headers="uploadHeaders"
        :on-success="handleSingleSuccess"
        :on-error="handleError"
        :before-upload="beforeUpload"
        :file-list="singleFileList">
        <el-button size="small" type="primary">点击上传</el-button>
        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过10MB</div>
      </el-upload>
    </el-card>

    <el-card class="box-card" style="margin-top: 20px;">
      <div slot="header" class="clearfix">
        <span>多文件上传测试</span>
      </div>
      <el-upload
        class="upload-demo"
        :action="uploadUrl"
        :headers="uploadHeaders"
        :on-success="handleMultipleSuccess"
        :on-error="handleError"
        :on-remove="handleRemove"
        :before-upload="beforeUpload"
        :file-list="multipleFileList"
        multiple>
        <el-button size="small" type="primary">点击上传</el-button>
        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过10MB</div>
      </el-upload>
    </el-card>

    <el-card class="box-card" style="margin-top: 20px;">
      <div slot="header" class="clearfix">
        <span>上传结果</span>
      </div>
      <pre>{{ uploadResult }}</pre>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "TestUpload",
  data() {
    return {
      uploadUrl: process.env.VUE_APP_BASE_API + '/common/uploads',
      uploadHeaders: {},
      singleFileList: [],
      multipleFileList: [],
      uploadResult: {}
    };
  },
  created() {
    this.initUploadHeaders();
  },
  methods: {
    initUploadHeaders() {
      this.uploadHeaders = {
        'Authorization': 'Bearer ' + this.$store.getters.token
      };
    },
    beforeUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
      const isLt10M = file.size / 1024 / 1024 < 10;

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG/PNG 格式!');
        return false;
      }
      if (!isLt10M) {
        this.$message.error('上传图片大小不能超过 10MB!');
        return false;
      }
      return true;
    },
    handleSingleSuccess(response, file, fileList) {
      console.log('Single upload success:', response);
      this.uploadResult = response;
      if (response.code === 200) {
        this.$message.success('单文件上传成功');
      } else {
        this.$message.error(response.msg || '单文件上传失败');
      }
    },
    handleMultipleSuccess(response, file, fileList) {
      console.log('Multiple upload success:', response);
      this.uploadResult = response;
      if (response.code === 200) {
        this.$message.success('多文件上传成功');
      } else {
        this.$message.error(response.msg || '多文件上传失败');
      }
    },
    handleError(error, file, fileList) {
      console.error('Upload error:', error);
      this.$message.error('文件上传失败');
    },
    handleRemove(file, fileList) {
      console.log('File removed:', file);
    }
  }
};
</script>

<style scoped>
.box-card {
  margin-bottom: 20px;
}

.upload-demo {
  margin: 20px 0;
}

pre {
  background-color: #f5f5f5;
  padding: 10px;
  border-radius: 4px;
  max-height: 300px;
  overflow-y: auto;
}
</style>
